package org.jeecgframework.web.material.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.material.entity.ProductStoreEntity;
import org.jeecgframework.web.material.service.ProductStoreServiceI;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;



/**   
 * @Description: 产品库存
 * @date 2015-08-19 09:51:59
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/productStoreController")
public class ProductStoreController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ProductStoreController.class);

	@Autowired
	private ProductStoreServiceI productStoreService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "productStore")
	public ModelAndView productStore(HttpServletRequest request) {
		return new ModelAndView("material/productStore/productStoreList");
	}
	
	/**
	 * 产品库存新增页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goAddOrUpdate")
	public ModelAndView goAdd(ProductStoreEntity productStore, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(productStore.getId())) {
			productStore = productStoreService.getEntity(ProductStoreEntity.class, productStore.getId());
			req.setAttribute("productStorePage", productStore);
		}
		return new ModelAndView("material/productStore/productStore");
	}
	

	/**
	 * 
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(ProductStoreEntity productStore,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(ProductStoreEntity.class, dataGrid);
		//查询条件组装器
		if(productStore.getProductId()!=null&&StringUtil.isNotEmpty(productStore.getProductId().getName())){
			productStore.getProductId().setName("*"+productStore.getProductId().getName()+"*");
		}
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, productStore, request.getParameterMap());
		try{
		//自定义追加查询条件
			TSUser user = ResourceUtil.getSessionUserName();
			cq.createAlias("companyId", "companyId");
			cq.eq("companyId.id", user.getCompany().getId());
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.productStoreService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}
	

	/**
	 * 更新产品库存
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doUpdate")
	@ResponseBody
	public AjaxJson doUpdate(ProductStoreEntity productStore, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		message = "库存预警设置成功";
		ProductStoreEntity t = productStoreService.get(ProductStoreEntity.class, productStore.getId());
		try {
			MyBeanUtils.copyBeanNotNull2Bean(productStore, t);
			productStoreService.saveOrUpdate(t);
			systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "库存预警设置失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

}
